import load from '@/utils/jsapi.js';

const echartutils = {
  async getechartdatas(mappoint) {
     const [
       geometryEngine,
       Polyline,
       SpatialReference
     ] = await load([
       "esri/geometry/geometryEngine",
       "esri/geometry/Polyline",
       "esri/geometry/SpatialReference",
     ]);
     let returnmessage=[];
    window.echartsdatas.map(function(item,ary,index){
      let sitem=item[1].filter(function(ite,ss,r){
         let paths = [];
         paths.push([mappoint.x, mappoint.y]);
         paths.push([ite.corrd[0], ite.corrd[1]]);
         const line = new Polyline({
           hasZ: false,
           hasM: true,
           paths: paths,
           spatialReference: {
             wkid: 102100
           }
         })
         const dist = geometryEngine.geodesicLength(line, 'meters');
         ite.distance=dist;
         if(dist<200) return true;
       })
       if (sitem.length)
       {
        const sortedchartdatas = sitem.sort(function(a, b) {
           return a.distance - b.distance;
         });
         if (sortedchartdatas.length)
          returnmessage.push([item[0], sortedchartdatas[0]]);        
       }
    })
    if (returnmessage.length)
    window.parent.postMessage(JSON.stringify({
      "action": "echartlayer",
      "layername": "echartlayer",
      "data": returnmessage
    }), '*')
  }
}
export default echartutils;
